<template>
  <div class="page flex flex-col gap-y-3 p-3 h-dvh">
    <n-card :bordered="false">
      <n-form
        label-align="left"
        label-placement="left"
        label-width="auto"
        :inline="true"
        :model="formParams"
        :show-feedback="false"
        :show-require-mark="false"
      >
        <n-grid responsive="screen" cols="1 s:1 m:2 l:3 xl:4 2xl:4" :collapsed="isCollapse" :x-gap="16" :y-gap="16">
          <n-form-item-gi label="登录账户"> <n-input v-model:value="formParams.loginUm" placeholder="请输入登录账户" /> </n-form-item-gi>
          <n-gi :suffix="true">
            <n-space align="center" justify="end">
              <n-button
                v-if="hasPermission('bmpus:online:usr:search', '查询在线用户列表')"
                type="primary"
                :loading="searchBtnLoading"
                @click="handleSearch"
              >
                <template #icon>
                  <n-icon>
                    <SearchOutlined />
                  </n-icon>
                </template>
                查询
              </n-button>
              <n-button @click="() => (formParams = new FormParams())">重置</n-button>
              <n-button type="primary" icon-placement="right" text @click="() => (isCollapse = !isCollapse)">
                <template #icon>
                  <n-icon size="14" v-if="isCollapse">
                    <DownOutlined />
                  </n-icon>
                  <n-icon size="14" v-else>
                    <UpOutlined />
                  </n-icon>
                </template>
                {{ isCollapse ? '展开' : '收起' }}
              </n-button>
            </n-space>
          </n-gi>
        </n-grid>
      </n-form>
    </n-card>
    <n-card :bordered="false" class="flex-1">
      <DataTable
        remote
        striped
        :row-key="(row: BMPUS.OnlineUsrQryOutput_OnlineUsrDto) => row.loginSid"
        :columns="tableColumns"
        :data="dataList"
        :pagination="pagination"
        :loading="dataLoading"
        :scroll-x="2000"
        @update:page="updatePage"
        @update:page-size="updatePageSize"
        @reload="loadData"
      >
      </DataTable>
    </n-card>
  </div>
</template>

<script lang="ts" setup>
import { h, reactive, ref, onMounted } from 'vue';
import { NSpace, NIcon, NButton, NPopconfirm } from 'naive-ui';
import type { DataTableColumns } from 'naive-ui';
import { removeEmptyField, formatDate } from '@yanfan/common/utils';
import DataTable, { getDefaultPagination } from '@/components/DataTable';
import type * as BMPUS from '@yanfan/bmpus-tsapi/types';
import { onlineUsrQry, onlineUsrExit } from '@yanfan/bmpus-tsapi/usr-chnl';
import { DownOutlined, UpOutlined, SearchOutlined, DeleteOutlined } from '@vicons/antd';
import { usePermission } from '@yanfan/common/hooks';

defineOptions({
  name: 'BmpusOnlineUsrList',
  titl: '在线用户列表',
});

const { hasPermission } = usePermission();

class FormParams {
  loginUm: string | null = null;
}

const tableColumns: DataTableColumns<BMPUS.OnlineUsrQryOutput_OnlineUsrDto> = [
  { title: '登录会话', key: 'loginSid', width: 180 },
  {
    title: '登录时间',
    key: 'loginTm',
    width: 180,
    render(rowData) {
      return formatDate(rowData.loginTm, 'YYYY-MM-DD HH:mm:ss');
    },
  },
  {
    title: '登录账户',
    key: 'loginUm',
    width: 150,
    ellipsis: {
      tooltip: true,
    },
  },
  { title: '登录IP', key: 'loginIpv4', width: 150 },
  {
    title: '登录地点',
    key: 'loginLoc',
    width: 200,
    ellipsis: {
      tooltip: true,
    },
  },
  { title: '操作系统', key: 'loginOs', width: 150 },
  { title: '浏览器', key: 'loginBs', width: 150 },
  { title: '登录类型名称', key: 'loginTpNm', width: 150 },
  { title: '登录状态名称', key: 'loginStNm', width: 150 },
  {
    title: '登录消息',
    key: 'loginMsg',
    width: 200,
    ellipsis: {
      tooltip: true,
    },
  },
  {
    title: '系统名称',
    key: 'sysNm',
    width: 200,
    ellipsis: {
      tooltip: true,
    },
  },
  { title: '备注', key: 'rmrk' },
  {
    title: '操作',
    key: 'actions',
    width: 140,
    align: 'center',
    fixed: 'right',
    render(row) {
      return h(NSpace, { align: 'center', justify: 'center' }, () => [
        h(
          NPopconfirm,
          { onPositiveClick: () => handleExit(row) },
          {
            trigger: () =>
              h(NButton, { type: 'error', text: true }, { icon: () => h(NIcon, { size: 18 }, () => h(DeleteOutlined)), default: () => '强退' }),
            default: () => '确定要强退吗？',
          },
        ),
      ]);
    },
  },
];

const isCollapse = ref(false);
const formParams = ref(new FormParams());
const searchBtnLoading = ref(false);
const pagination = reactive(getDefaultPagination());
const dataLoading = ref(false);
const dataList = ref<BMPUS.OnlineUsrQryOutput_OnlineUsrDto[]>([]);

onMounted(() => {
  loadData();
});

function updatePage(page: number) {
  pagination.page = page;
  loadData();
}

function updatePageSize(pageSize: number) {
  pagination.page = 1;
  pagination.pageSize = pageSize;
  loadData();
}

function loadData() {
  dataLoading.value = true;
  onlineUsrQry({ pageIndx: pagination.page, pageSize: pagination.pageSize, ...removeEmptyField(formParams.value) })
    .then((data) => {
      dataList.value = data.list;
      pagination.itemCount = data.totl;
    })
    .finally(() => {
      dataLoading.value = false;
      searchBtnLoading.value = false;
    });
}

function handleSearch() {
  searchBtnLoading.value = true;
  pagination.page = 1;
  loadData();
}

function handleExit(row: BMPUS.OnlineUsrQryOutput_OnlineUsrDto) {
  onlineUsrExit({ loginSid: row.loginSid }).then(() => {
    window.$message.success('强退成功');
    loadData();
  });
}
</script>

<style lang="scss" scoped></style>
